package com.njust.diary.Controlller;

import cn.hutool.core.util.StrUtil;
import com.njust.diary.po.Comment;
import com.njust.diary.po.Note;
import com.njust.diary.po.NoteType;
import com.njust.diary.po.User;
import com.njust.diary.service.NoteService;
import com.njust.diary.service.NoteTypeService;
import com.njust.diary.vo.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/modify")
public class NoteServlet extends HttpServlet {

  NoteService noteService =new NoteService();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String actionName=req.getParameter("actionName");
            if("modify".equals(actionName)){

                noteView(req,resp);

            }else if("addOrUpdate".equals(actionName)){
                addOrUpdate(req,resp);

        }else if("detail".equals(actionName)){
                noteDetail(req,resp);

            }else if("delete".equals(actionName)){
                noteDelete(req,resp);

            }else if("addComment".equals(actionName)){
                addComment(req,resp);
            }else if("unlike".equals(actionName)){
                unLike(req,resp);
            }else if("like".equals(actionName)){
                Like(req,resp);
            }


    }

    private void unLike(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("islike",0);
       User user=(User)req.getSession().getAttribute("user");
       Integer userId=user.getUserId();
       String noteId=req.getParameter("noteId");
        Integer noteId2=Integer.valueOf(noteId);
        noteService.deleteLike(userId,noteId2);


        Note note =noteService.findNoteById(noteId);
        req.setAttribute("note",note);
        String uname1=user.getUname();
        String uname2=note.getUname();
        if(uname1.equals(uname2)){
            req.setAttribute("selfcode",1);
        }else{
            req.setAttribute("selfcode",0);
        }
        List<Comment> comList = noteService.findComList(noteId);
        req.setAttribute("comList",comList);//显示评论
        Integer islike =noteService.findLikebyUserId(userId);
        if(islike>0){
            req.setAttribute("islike",1);
        }else{
            req.setAttribute("islike",0);
        }
        Long totallike=noteService.findLikebyNoteId(noteId);
        req.setAttribute("totallike",totallike);//显示点赞

        req.setAttribute("changePage","notedetail/detail.jsp");
        req.getRequestDispatcher("index.jsp").forward(req,resp);
    }
    private void Like(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("islike",1);
        User user=(User)req.getSession().getAttribute("user");
        Integer userId=user.getUserId();
        String noteId=req.getParameter("noteId");
        Integer noteId2=Integer.valueOf(noteId);
        noteService.insertLike(userId,noteId2);


        Note note =noteService.findNoteById(noteId);
        req.setAttribute("note",note);
        String uname1=user.getUname();
        String uname2=note.getUname();
        if(uname1.equals(uname2)){
            req.setAttribute("selfcode",1);
        }else{
            req.setAttribute("selfcode",0);
        }
        List<Comment> comList = noteService.findComList(noteId);
        req.setAttribute("comList",comList);//显示评论
        Integer islike =noteService.findLikebyUserId(userId);
        if(islike>0){
            req.setAttribute("islike",1);
        }else{
            req.setAttribute("islike",0);
        }
        Long totallike=noteService.findLikebyNoteId(noteId);
        req.setAttribute("totallike",totallike);//显示点赞

        req.setAttribute("changePage","notedetail/detail.jsp");
        req.getRequestDispatcher("index.jsp").forward(req,resp);
    }
    private void addComment(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String content=req.getParameter("comment");
        String noteId=req.getParameter("noteId");
        User user=(User) req.getSession().getAttribute("user");
        String uname=user.getUname();
       Result<Comment> result =noteService.addComment(noteId,content,uname);
        req.setAttribute("result",result);
       Integer userId=user.getUserId();

        Note note =noteService.findNoteById(noteId);
        req.setAttribute("note",note);
        String uname1=user.getUname();
        String uname2=note.getUname();
        if(uname1.equals(uname2)){
            req.setAttribute("selfcode",1);
        }else{
            req.setAttribute("selfcode",0);
        }
        List<Comment> comList = noteService.findComList(noteId);
        req.setAttribute("comList",comList);
        Integer islike =noteService.findLikebyUserId(userId);
        if(islike>0){
            req.setAttribute("islike",1);
        }else{
            req.setAttribute("islike",0);
        }
        Long totallike=noteService.findLikebyNoteId(noteId);
        req.setAttribute("totallike",totallike);

        req.setAttribute("changePage","notedetail/detail.jsp");
        req.getRequestDispatcher("index.jsp").forward(req,resp);


    }
    private void addOrUpdate(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String typeId =req.getParameter("typeId");
        String title =req.getParameter("title");
        String content =req.getParameter("content");
        String noteId =req.getParameter("noteId");
        String lon =req.getParameter("lon");
        String lat =req.getParameter("lat");
        User user = (User)req.getSession().getAttribute("user");
        String uname =user.getUname();

        Result<Note> result =noteService.addOrUpdate(typeId,title,content,noteId,lon,lat,uname);
        if(result.getCode()==1){//新建
            resp.sendRedirect("index");
        }else{//更新
            req.setAttribute("result",result);
            String url ="modify?actionName=modify";
            if(!StrUtil.isBlank(noteId)){
                url += "&noteId="+noteId;
            }
            req.getRequestDispatcher(url).forward(req,resp);

        }
    }
    private void noteDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
   String noteId = req.getParameter("noteId");
   Integer code =noteService.deleteNote(noteId);
   resp.getWriter().write(code+"");
   resp.getWriter().close();



    }
    private void noteView(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String noteId =req.getParameter("noteId");
        Note note =noteService.findNoteById(noteId);
        req.setAttribute("noteInfo",note);
        User user = (User) req.getSession().getAttribute("user");
        List<NoteType> typeList = new NoteTypeService().findTypeList(user.getUserId());
        req.setAttribute("typeList",typeList);



        req.setAttribute("menupage","modify");

        req.setAttribute("changePage","modify/modify.jsp");
        req.getRequestDispatcher("index.jsp").forward(req,resp);


    }
    private void noteDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         String noteId = req.getParameter("noteId");
         Note note =noteService.findNoteById(noteId);
         req.setAttribute("note",note);
         User user=(User)req.getSession().getAttribute("user");
         String uname1=user.getUname();
         String uname2=note.getUname();
         if(uname1.equals(uname2)){
             req.setAttribute("selfcode",1);
         }else{
             req.setAttribute("selfcode",0);
         }
        List<Comment> comList = noteService.findComList(noteId);
        req.setAttribute("comList",comList);
        Integer userId =user.getUserId();
      Integer islike =noteService.findLikebyUserId(userId);
    if(islike>0){
        req.setAttribute("islike",1);
    }else{
        req.setAttribute("islike",0);
    }
Long totallike=noteService.findLikebyNoteId(noteId);
        req.setAttribute("totallike",totallike);



         req.setAttribute("changePage","notedetail/detail.jsp");
         req.getRequestDispatcher("index.jsp").forward(req,resp);


    }




}
